package com.zyiot.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zyiot.cache.ImageDownloader;
import com.zyiot.cache.OnImageDownload;
import com.zyiot.lbs.R;
import com.zyiot.model.ContentModel;

public class ContentAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<ContentModel> items;
	public static final int MSG_NET_TIMEOUT = 100;
	public static final int MSG_NET_STATUS_ERROR = 200;
	public static final int MSG_NET_SUCC = 1;
	private ViewHolder holder;
	private boolean mBusy = false;
	private Context context;
	Activity mActivity;
	ListView mListView;
	// private ImageLoader mImageLoader;
	ImageDownloader mDownloader;

	public void setFlagBusy(boolean busy) {
		this.mBusy = busy;
	}

	/* class ViewHolder */
	private class ViewHolder {
		TextView addr;
		TextView name;
		TextView distance;
		TextView comments;
		TextView likes;
		ImageView icon;
	}

	public ContentAdapter(Context context, List<ContentModel> list, Activity mActivity, ListView mListView) {
		mInflater = LayoutInflater.from(context);
		items = list;
		this.context = context;
		this.mActivity = mActivity;
		this.mListView = mListView;
		// mImageLoader = new ImageLoader(context);
	}

	// public ImageLoader getImageLoader() {
	// return mImageLoader;
	// }

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.content_eshop, null);
			holder = new ViewHolder();

			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.addr = (TextView) convertView.findViewById(R.id.addr);
			holder.distance = (TextView) convertView.findViewById(R.id.distance);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon_eshop_logo);
			holder.comments = (TextView) convertView.findViewById(R.id.comments);
			holder.likes = (TextView) convertView.findViewById(R.id.likes);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.addr.setText(items.get(position).getAddr() + "");
		holder.name.setText(items.get(position).getName() + "");
		holder.distance.setText(items.get(position).getDistance() + "");
		holder.comments.setText(items.get(position).getComments() + "");
		holder.likes.setText(items.get(position).getLikes() + "");

		if (mDownloader == null) {
			mDownloader = new ImageDownloader();
		}

		String url = "";
		url = items.get(position).getImageurl();
		holder.icon.setTag(url);
		// 这句代码的作用是为了解决convertView被重用的时候，图片预设的问题
		holder.icon.setImageResource(R.drawable.default_business);
		String imagePath = "/zyiot";

		if (mDownloader != null && !url.equals("")) {
			mDownloader.imageDownload(url, holder.icon, imagePath, mActivity, new OnImageDownload() {
				@Override
				public void onDownloadSucc(Bitmap bitmap, String c_url) {
					if (!c_url.equals("")) {
						ImageView imageView = (ImageView) mListView.findViewWithTag(c_url);
						if (imageView != null && c_url.equals(imageView.getTag())) {
							imageView.setImageBitmap(bitmap);
							imageView.setTag("");
						} else if (imageView != null) {
							imageView.setImageResource(R.drawable.default_business);
						}
					}
				}
			});
		}
		return convertView;
	}

}
